/**
 * Apr 9, 2010
 */
package org.tomale.id.ui.preference.pages;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.tomale.id.ui.Activator;

/**
 * @author ferd
 *
 */
public class CompanyInfoPage extends PreferencePage implements
		IWorkbenchPreferencePage {
	
	Text _company;
	Text _address;
	
	public CompanyInfoPage(){
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		noDefaultAndApplyButton();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		l = new Label(comp, SWT.NONE);
		l.setText("Company Name");
		l.setLayoutData(new GridData());
		
		_company = new Text(comp, SWT.BORDER);
		_company.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		
		l = new Label(comp, SWT.NONE);
		l.setText("Address");
		l.setLayoutData(new GridData());
		
		_address = new Text(comp,SWT.BORDER);
		_address.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));

		setPreferences();
		
		return comp;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {

	}

	@Override
	public boolean isValid() {
		return true;
	}

	@Override
	public boolean performOk() {
		save();
		return super.performOk();
	}
	
	private void save(){
		if(isValid()){
			IPreferenceStore store = getPreferenceStore();
			store.setValue(PreferenceConstants.COMPANY_NAME, _company.getText());
			store.setValue(PreferenceConstants.COMPANY_ADDR, _address.getText());
		}
	}
	
	private void setPreferences(){
		IPreferenceStore store = getPreferenceStore();
		_company.setText(store.getString(PreferenceConstants.COMPANY_NAME));
		_address.setText(store.getString(PreferenceConstants.COMPANY_ADDR));
	}
}
